package adapter;


import utils.Category;

import com.example.lastprojectbeautiplus.R;
import com.squareup.picasso.Picasso;

import android.app.Activity;
import android.content.Context;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class HorizontalListViewAdapter extends BaseAdapter{
	Category[] cateList;
	Context context;
	TextView tvCategory;
	ImageView imgCategory;
	
	public HorizontalListViewAdapter(Context context, Category[] cateList){
		this.context = context;
		this.cateList = cateList;
	}
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return cateList.length;
	}
	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return cateList[position];
	}
	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		View view = convertView;
		if (view == null) {
            LayoutInflater mInflater = (LayoutInflater)
                    context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            convertView = mInflater.inflate(R.layout.horizontal_list_item, null);
        }
		else
			view = (View) convertView;
		DisplayMetrics metrics = convertView.getResources().getDisplayMetrics();
        int height, width;
        width = metrics.widthPixels;
        height = metrics.heightPixels;
        
		tvCategory = (TextView) convertView.findViewById(R.id.tvCategoryName);
		imgCategory = (ImageView) convertView.findViewById(R.id.imgCategory);
		tvCategory.setText(cateList[position].getCategoryName());
		imgCategory.getLayoutParams().height = height/4;
		imgCategory.getLayoutParams().width = width/4;
		String url = null;
		if(cateList[position].getImgpath() != null)
			url= cateList[position].getImgpath();
		Picasso.with(context).load(url)
			.placeholder(R.drawable.image_wait)
			.error(R.drawable.error_loading)
			.into(imgCategory);
		return convertView;
	}
}
